const Mongoose = require("./db");
const account = require("../counts/account.js");
const formatDateHelper = require('../utils/formatDateHelper.js')
const accountSchema = new Mongoose.Schema({
	id: {
		type: Number,
		unique: true,
	},
	username: String,
	phone: String,
	password: String,
	comfirm_password: String,
	role: Number,
	room_id: Number,
	car_id: Number,
	car_number: String,
	status: Number,
	create: {
		type: String,
		default: formatDateHelper.date() 
	},
	modify: {
		type: String,
		default: formatDateHelper.date()
	}
})


accountSchema.pre("save", async function(next) {
	const self = this;
	try {
		if (!self.id) {
			self.id = await account.getNextSequenceValue("id");
		}
	} catch (err) {
		next(err);
	}
});

const accountModel = Mongoose.model("accounts", accountSchema);

module.exports = accountModel;